import { Component } from '@angular/core';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../../providers/http/http';
import { StorageProvider } from '../../../providers/storage/storage';
import { DialogProvider } from '../../../providers/dialog/dialog';
import { UserInfoProvider } from '../../../providers/user-info/user-info';



/**
 * Generated class for the MyCouponsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-pay-my-coupons',
  templateUrl: 'pay-my-coupons.html',
})
export class PayMyCouponsPage {

  member_id: any;
  //用户信息
  userInfo: user_info = new user_info();

  public Coupons: Coupons[] = new Array<Coupons>();

  coupon: Coupons = {
    member_coupon_id: '',
    member_id: '',
    coupon_id: '',
    created_at: '',
    updated_at: '',
    cuser_id: '',
    uuser_id: '',
    is_use: '',
    use_coupon_tiem: '',
    remarks: '',
    ma_coupon: {
      coupon_id: '',
      coupon_type: '',
      coupon_name: '',
      denomination: '',
      stock: '',
      expiry_date: '',
      is_issuance: '',
      created_at: '',
      updated_at: '',
      cuser_id: '',
      uuser_id: '',
      is_del: '',
    }

  };

  constructor(
    public navCtrl: NavController,
    public viewCtrl: ViewController,
    public navParams: NavParams,
    public http: HttpProvider,
    public storage: StorageProvider,
    public dialog: DialogProvider,
    public user: UserInfoProvider) { }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MyCouponsPage');
    this.userInfo = this.user.getuserInfo();
    this.member_id = this.userInfo.member_id;
    this.getCoupons();
    // this.storage.getStorage("login_info").then(res => {
    // 	console.log(res);
    // 	if (res != undefined) {
    // 		this.member_id = res['member_id']
    // 		this.getCoupons();
    // 	}
    // });
  }

  getCoupons() {
    this.http.postJson('', 'member/memberco/index?page=1', {
      "where": ['and', ["=", "member_id", this.member_id], ["=", "is_use", '0']],
      "limit": 15,
      "joinwith": ["ma_coupon"]
    }, '').then(res => {
      console.log(res)
      if (res['status'] == 200) {
        this.getMessage(res['data']);
      }
    });
  }


  getMessage(Coupons){
    this.Coupons.splice(0);
    console.log(Coupons)
    
      for(let coupons of Coupons){
        if(coupons.ma_coupon){
           var remarks = coupons.ma_coupon.remarks;
          
           if(coupons.surplus_num >1){
               
               // expiry_date:(parseInt(coupons.created_at) + parseInt(coupons.ma_coupon.expiry_date)*24*60*60)*1000,//券有效期
               coupons.ma_coupon.expiry_date = parseInt(coupons.overdue_time)*1000;//券有效期
               coupons.ma_coupon.denomination = coupons.ma_coupon.denomination;//代金券面额
               coupons.ma_coupon.coupon_name = coupons.ma_coupon.coupon_name;//卷名称
               coupons.remarks = coupons.ma_coupon.remarks;//备注
              
           }else{ 
               // expiry_date:(parseInt(coupons.created_at) + parseInt(coupons.ma_coupon.expiry_date)*24*60*60)*1000,//券有效期
               coupons.ma_coupon.expiry_date = parseInt(coupons.overdue_time)*1000;//券有效期
               coupons.ma_coupon.denomination = coupons.ma_coupon.denomination;//代金券面额
               coupons.ma_coupon.coupon_name = coupons.ma_coupon.coupon_name;//卷名称
               coupons.remarks = coupons.remarks;//备注

           }
          
          if(new Date().getTime() < coupons.ma_coupon.expiry_date){
           this.Coupons.push(coupons);
          }
          }
      }
  }

  select(data: Coupons) {
    if (this.coupon.ma_coupon.coupon_id !== data.ma_coupon.coupon_id) {
      this.coupon = data;
    } else {
      this.coupon = {
        member_coupon_id: '',
        member_id: '',
        coupon_id: '',
        created_at: '',
        updated_at: '',
        cuser_id: '',
        uuser_id: '',
        is_use: '',
        use_coupon_tiem: '',
        remarks: '',
        ma_coupon: {
          coupon_id: '',
          coupon_type: '',
          coupon_name: '',
          denomination: '',
          stock: '',
          expiry_date: '',
          is_issuance: '',
          created_at: '',
          updated_at: '',
          cuser_id: '',
          uuser_id: '',
          is_del: '',
        }
      }
      };

    }

    okClick(){
      if (this.coupon.ma_coupon.coupon_id !== ''){
        this.viewCtrl.dismiss({ data: this.coupon.ma_coupon });
      } else {
        this.dialog.showAlert('提示','没有选择优惠券');
      }
      
    }
    noClick(){
      this.viewCtrl.dismiss();
    }

  }

export class user_info {
  member_id: any;
  nickname: any;
  phone: any;
  balance: any;
  local_balance: any;
  member_state: any;
  created_at: any;
  updated_at: any;
  cuser_id: any;
  uuser_id: any;
  is_del: any;
  password: any;
  file_type_id: any;
  access_token: any;
  isAutomaticLogon: any;
}

export class Coupons {
  member_coupon_id: any;
  member_id: any;
  coupon_id: any;
  created_at: any;
  updated_at: any;
  cuser_id: any;
  uuser_id: any;
  is_use: any;
  use_coupon_tiem: any;
  remarks: any;
  ma_coupon: {
    coupon_id: any;
    coupon_type: any;
    coupon_name: any;
    denomination: any;
    stock: any;
    expiry_date: any;
    is_issuance: any;
    created_at: any;
    updated_at: any;
    cuser_id: any;
    uuser_id: any;
    is_del: any;
  }

}